FROM golang:alpine3.14 AS build
ENV GOPROXY=https://goproxy.cn \ 
    GO111MODULE=on \
    GOOS=linux

#增加工作目录，不然会提示「$GOPATH/go.mod exists but should not」
WORKDIR /app
ADD go.mod .    
COPY *.go .
RUN go mod download && \
    go mod tidy && \
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /httpserver

FROM alpine
COPY --from=build /httpserver /httpserver
RUN echo '@aliyun http://mirrors.aliyun.com/alpine/edge/community' > /etc/apk/repositories && \
    echo 'http://mirrors.aliyun.com/alpine/v3.9/main/' >> /etc/apk/repositories && \
    echo 'http://mirrors.aliyun.com/alpine/v3.9/community/' >> /etc/apk/repositories && \
    apk update && \
    apk upgrade && \
    apk add --no-cache curl &&\
    rm -rf /tmp/* /var/cache/apk/*
EXPOSE 8080
ENTRYPOINT /httpserver
HEALTHCHECK --start-period=5s --timeout=3s CMD curl -f http://localhost:8080/healthz || exit 1